import java.io.File;
import java.util.ArrayList;
import java.util.Date;

/**
 * @(#) Project.java
 */

public class Project
{
	private String name;
	
	private Date StartTime;
	
	private Date EndTime;
	
	private Task Creates;
	
	private GraphViz Draw;
	
	public File2 LookForFile;
	
	public ArrayList<Task> tasks = new ArrayList<Task>();
	public ArrayList<Task> isOnCritical = new ArrayList<Task>();
	
	public void Create(String name, Date startDate, Date endDate )
	{
		this.name = name;
		this.StartTime = startDate;
		this.EndTime = endDate;
	}
	
	public void CreateGraph()
	{
		GraphViz gv = new GraphViz();
		gv.addln(gv.start_graph());
		gv.addln("node [ fontname=\"Helvetica-Bold\", shape=\"rectangle\", width=\"0.3\", style =\"setlinewidth(2),filled\" color=\"black\", fillcolor = \"pink\"];");
		gv.addln("edge [ fontname=\"Helvetica-Bold\", fontsize=\"16\", style=\"bold\" ];");
		gv.addln("graph [ rankdir=\"LR\", ranksep=\"0.4\"];");
		ArrayList<String> isOnCritical = new ArrayList<String>();
		try{
		for (Task t : this.tasks) {
			String toAdd = "\""+t.TaskName+"\"";
			if(t.Create.size() < 1){
				gv.addln(toAdd);
			} else {
				int biggestValue=0;
				String hasBiggest="";
				for (int i = 0; i < t.Create.size(); i++){
					int value2 = 0;
					for (Task t2 : this.tasks){
						if(t2.TaskName.equals(t.Create.get(i).name)){
							value2 = t2.NormalDuration;
						}
					}
					if(value2>biggestValue){
						biggestValue=value2;
						hasBiggest = t.Create.get(i).name;
					}
				}
				for (int i = 0; i < t.Create.size(); i++){
					toAdd = "\""+t.TaskName+"\"";
					if((t.Create.get(i).name == hasBiggest && isOnCritical.contains(t.TaskName)) || (t.Create.get(i).name == hasBiggest && isOnCritical.isEmpty())){						
						toAdd+=" -> \"" + t.Create.get(i).name + "\" [ label = \""+ t.NormalDuration +"\", color=\"red\"];";
						isOnCritical.add(t.Create.get(i).name);
					}else{
						toAdd+=" -> \"" + t.Create.get(i).name + "\" [ label = \""+ t.NormalDuration +"\"];";
					}
					gv.addln(toAdd);
				}
			}
		}
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		gv.addln(gv.end_graph());
//		System.out.println(gv.getDotSource());
		String type = "png";
		File out = new File("C:/out." + type);   // Windows
		gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
		System.out.println("Done!");
	}	
}
